package com.chen.seckill.controller;

import com.chen.seckill.controller.vo.ItemVO;
import com.chen.seckill.error.BusinessException;
import com.chen.seckill.response.CommonReturnType;
import com.chen.seckill.service.ItemService;
import com.chen.seckill.service.bo.ItemBO;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/item")
@CrossOrigin(origins = {"*"}, allowCredentials = "true")
public class ItemController extends BaseController {

    @Autowired
    private ItemService itemService;

    // 商品列表页浏览
    @GetMapping("/list")
    @ResponseBody
    public CommonReturnType listItem() {
        List<ItemBO> itemBOList = itemService.listItem();
        List<ItemVO> itemVOList = itemBOList.stream().map(itemBO -> {
            ItemVO itemVO = convertVOFromBO(itemBO);
            return itemVO;
        }).collect(Collectors.toList());
        return CommonReturnType.create(itemVOList);
    }

    // 商品详情页浏览
    @GetMapping("/get")
    @ResponseBody
    public CommonReturnType getItem(@RequestParam(name = "id")Integer id) {
        ItemBO itemBO = itemService.getItemById(id);
        ItemVO itemVO = convertVOFromBO(itemBO);

        return CommonReturnType.create(itemVO);
    }

    // 创建商品的controller
    @PostMapping("/create")
    @ResponseBody
    public CommonReturnType createItem(@RequestParam(name = "title")String title,
                                       @RequestParam(name = "description")String description,
                                       @RequestParam(name = "price") BigDecimal price,
                                       @RequestParam(name = "stock") Integer stock,
                                       @RequestParam(name = "imgUrl") String imgUrl) throws BusinessException {
        ItemBO itemBO = new ItemBO();
        itemBO.setTitle(title);
        itemBO.setDescription(description);
        itemBO.setPrice(price);
        itemBO.setStock(stock);
        itemBO.setImgUrl(imgUrl);
        ItemBO itemBOForReturn = itemService.createItem(itemBO);

        ItemVO itemVO = convertVOFromBO(itemBOForReturn);

        return CommonReturnType.create(itemVO);

    }

    // 将ItemBO转化为ItemVO
    public ItemVO convertVOFromBO(ItemBO itemBO) {
        if (itemBO == null) {
            return null;
        }
        ItemVO itemVO = new ItemVO();
        BeanUtils.copyProperties(itemBO, itemVO);
        if (itemBO.getPromoBO() != null) {
            // 有正在进行或即将进行的秒杀活动
            itemVO.setPromoStatus(itemBO.getPromoBO().getStatus());
            itemVO.setPromoId(itemBO.getPromoBO().getId());
            itemVO.setStartDate(itemBO.getPromoBO().getStartDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
            itemVO.setPromoPrice(itemBO.getPromoBO().getPromoItemPrice());
        } else {
            itemVO.setPromoStatus(0);
        }
        return itemVO;
    }

}
